<?php
namespace app\shop\model;

use app\BaseModel;
use think\facade\Db;
class AdminStore extends BaseModel
{
	//定义表名

	protected $name = 'longbing_card_shop_store';


    /**
     * @author chenniang
     * @DataTime: 2020-06-22 13:28
     * @功能说明:转换图片格式
     */
	public function getImgAttr($value,$data){

	    if(!empty($value)){

            $data = transImages([$value],['img']);

            return !empty($data[0])?$data[0]:[];
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-22 09:25
     * @功能说明:添加自提点
     */
	public function storeAdd($data){

	    $data['create_time'] = time();

	    $data['update_time'] = time();

	    $data['status']      = 1;

	    $res = $this->insert($data);

	    return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-22 09:27
     * @功能说明:修改自提点
     */
    public function storeUpdate($dis,$data){

        $data['update_time'] = time();

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-22 09:29
     * @功能说明:自提列表
     */
    public function storeList($dis,$where,$page){

        $res = $this->where($dis)
                ->where(function ($query) use ($where){
                $query->whereOr($where);
                })->order('id desc')
                ->paginate($page)
                ->toArray();

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-22 09:46
     * @功能说明:自提点详情
     */
    public function storeInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-22 13:25
     * @功能说明:转换数据
     */
    public function changeData($data){

        $data['img'] = !empty($data['img'])?implode(',',$data['img']):'';

        return $data;
    }



    /**
     * @author chenniang
     * @DataTime: 2020-06-22 09:25
     * @功能说明:添加自提点
     */
    public function storeAddAdmin($data){

        $data['create_time'] = time();

        $data['update_time'] = time();

        $data['status']      = 1;

        $company = $data['company'];

        unset($data['company']);

        $res = $this->insert($data);

        $id = $this->getLastInsID();

        $this->updateSome($id,$data['uniacid'],$company);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-11 15:28
     * @功能说明:
     */
    public function updateSome($id,$uniacid,$company){

        if(!empty($company)){

            $store_company_model = new StoreCompany();

            $store_company_model->where(['store_id'=>$id])->delete();

            $company = $this->changeDataSome($company);

            foreach ($company as $k => $value){

                $insert[$k] = [

                    'uniacid' => $uniacid,

                    'store_id'=> $id,

                    'company_id' => $value
                ];

                $store_company_model->saveAll($insert);

            }
        }

        return true;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-06-22 09:27
     * @功能说明:修改自提点
     */
    public function storeUpdateAdmin($dis,$data){

        $data['update_time'] = time();

        $company = $data['company'];

        unset($data['company']);

        $res = $this->where($dis)->update($data);


        $this->updateSome($data['id'],$data['uniacid'],$company);

        return $res;

    }

    /**
     * User: chenniang
     * Date: 2019-09-25 15:29
     * @return void
     * descption:转换插入数据
     */
    public function changeDataSome($ids){
        $data = [];
        if(is_array($ids)){

            foreach ($ids as $k =>$v){
                $data[] = array_pop($v);
            }
        }

        return $data;
    }







}